package com.zxbit.uangstore.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cb.ratingbar.CBRatingBar;
import com.cloud.progressbar.ProgressButton;
import com.facebook.drawee.view.SimpleDraweeView;
import com.zxbit.uangstore.R;
import com.zxbit.uangstore.base.BaseRecyclerAdapter;
import com.zxbit.uangstore.domain.ProductItemData;
import com.zxbit.uangstore.utils.DebugLog;
import com.zxbit.uangstore.utils.PackageUtils;
import com.zxbit.uangstore.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

@SuppressLint("CheckResult")
public class ProductAdapter extends BaseRecyclerAdapter<ProductItemData, RecyclerView.ViewHolder> {

    private final int TYPE_SEARCH = 0;
    private final int TYPE_NORMAL = 1;
    private String CASHLOAN = "cashloan";
    private String INSTALLMENT = "installment";

    private Context context;
    private OnProductListener listener;

    public ProductAdapter(Context context, OnProductListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? TYPE_SEARCH : TYPE_NORMAL;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_SEARCH:
                return new SearchViewHolder(inflater.inflate(R.layout.item_of_search_layout, parent, false));
            case TYPE_NORMAL:
                return new ProductViewHolder(inflater.inflate(R.layout.item_of_product_layout, parent, false));
        }
        return null;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int position) {
        if (position == TYPE_SEARCH) {
            SearchViewHolder searchViewHolder = (SearchViewHolder) viewHolder;
            searchViewHolder.btnCashLoan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onTabClick(CASHLOAN);
                }
            });

            searchViewHolder.btnInstallment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onTabClick(INSTALLMENT);
                }
            });
        } else {
            ProductViewHolder productViewHolder = (ProductViewHolder) viewHolder;
            final ProductItemData productItemData = mDatas.get(position);
            productViewHolder.tv_borrowing_range.setVisibility(View.GONE);
            productViewHolder.layout_installment.setVisibility(View.GONE);

            productViewHolder.tv_product_name.setText(productItemData.getProductName());
            if ("1".equals(productItemData.getIsboutique())) { // 是否精选
                productViewHolder.tv_boutique.setVisibility(View.VISIBLE);
            } else {
                productViewHolder.tv_boutique.setVisibility(View.GONE);
            }
            if (!StringUtils.isEmpty(productItemData.getLogoUrl())) {
                productViewHolder.img_logo.setImageURI(productItemData.getLogoUrl());
            }
            if (!StringUtils.isEmpty(productItemData.getScore())) {
                productViewHolder.tv_score.setText(productItemData.getScore());
                productViewHolder.star_score.setStarProgress(Float.parseFloat(productItemData.getScore()));
            }
            productViewHolder.tv_advertising_slogan.setText(productItemData.getAdvertisingSlogan());
            productViewHolder.tv_introduction.setText(productItemData.getIntroduction());

            if (CASHLOAN.equals(productItemData.getType())) {
                productViewHolder.tv_borrowing_range.setVisibility(View.VISIBLE);
                String borrowingRangeStart = productItemData.getBorrowingRangeStart();
                String borrowingRangeEnd = productItemData.getBorrowingRangeEnd();
                String separator = "-"; // 分隔符  专治后台瞎传数据
                if (borrowingRangeStart.indexOf(separator) == borrowingRangeStart.length() - 1
                        || borrowingRangeEnd.indexOf(separator) == 0) {
                    separator = "";
                }
                productViewHolder.tv_borrowing_range.setText(StringUtils.getDotMoneyStr(productItemData.getBorrowingRangeStart())
                        + separator + StringUtils.getDotMoneyStr(productItemData.getBorrowingRangeEnd()));

            } else if (INSTALLMENT.equals(productItemData.getType())) {
                productViewHolder.layout_installment.setVisibility(View.VISIBLE);
                String downPayment = productItemData.getDownPayment();
                if (!downPayment.contains("%")) downPayment += "%";
                productViewHolder.tv_down_payment.setText(downPayment);
                String monthlyInterestRate = productItemData.getMonthlyInterestRate();
                if (!monthlyInterestRate.contains("%")) monthlyInterestRate += "%";
                productViewHolder.tv_monthly_interest_rate.setText(monthlyInterestRate);
                productViewHolder.tv_staging_period.setText(productItemData.getStagingPeriod()
                        + context.getResources().getString(R.string.main_month_unit));
            }

            productViewHolder.btn_download.setVisibility(View.VISIBLE);

            final boolean isInstall = PackageUtils.checkApkExist(context, productItemData.getPackageName());
            if(isInstall) {
                productViewHolder.btn_download.setText(context.getResources().getString(R.string.main_product_open));
                productViewHolder.layout_btn.setBackgroundResource(R.drawable.them_item_select);
            } else {
                productViewHolder.btn_download.setText(context.getResources().getString(R.string.main_product_down));
                productViewHolder.layout_btn.setBackgroundResource(R.drawable.them_item_no_select);
            }

            productViewHolder.btn_download.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onHistoryClick(view, productItemData,isInstall);
                }
            });
        }
    }

    class SearchViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.btn_cash_loan)
        TextView btnCashLoan; // 现金
        @BindView(R.id.btn_installment)
        TextView btnInstallment; // 分期

        SearchViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ProductViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_logo)
        SimpleDraweeView img_logo; // LOGO
        @BindView(R.id.tv_product_name)
        TextView tv_product_name; // APP名称
        @BindView(R.id.tv_boutique)
        TextView tv_boutique; // 精选
        @BindView(R.id.tv_score)
        TextView tv_score; // 评分
        @BindView(R.id.star_score)
        CBRatingBar star_score;
        @BindView(R.id.layout_btn)
        RelativeLayout layout_btn;
        @BindView(R.id.btn_download)
        ProgressButton btn_download;
        @BindView(R.id.tv_borrowing_range)
        TextView tv_borrowing_range;
        @BindView(R.id.tv_advertising_slogan)
        TextView tv_advertising_slogan;
        @BindView(R.id.layout_installment)
        LinearLayout layout_installment;
        @BindView(R.id.tv_down_payment)
        TextView tv_down_payment;
        @BindView(R.id.tv_monthly_interest_rate)
        TextView tv_monthly_interest_rate;
        @BindView(R.id.tv_staging_period)
        TextView tv_staging_period;
        @BindView(R.id.tv_introduction)
        TextView tv_introduction;

        ProductViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnProductListener {
        void onHistoryClick(View btn, ProductItemData itemData, boolean isInstall);
        void onTabClick(String type);
    }

}
